<?php 
// Library files
include '../includes/cPlayer.php';
include '../includes/cMember.php';
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cGame.php';
include '../includes/cDates.php';
include '../includes/cPage.php';

define("TD_NAME", "<td width='100' align='center'>");

$cPage = new page("Ericsson Cup: Instructions");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$cDates = new dates();
$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);

$numMembers = mysqli_num_rows($result);
$numberRounds = CalculateNumberRounds($numMembers);

$query = "SELECT * FROM tandbergCup LIMIT 1";
$result = mysqli_query($connect, $query);
if ($result == 0)
{
	echo "<BR>The draw for the Ericsson Cup has not yet been done for this season.";
}
else
{
	$row = mysqli_fetch_array($result, MYSQLI_NUM);

	$weekArray = array();
	for ($round = 1; $round <= $numberRounds; $round++)
	{
		$weekArray[$round] = $row[$round+4]; 	
	}

	$endMonth = $cDates->getEndMonth();
	if ($endMonth < $cDates->getStartMonth())
	{
		// It's next year
		$endMonth = $cDates->getEndMonth()+12;
	}



	echo "<BR>The table below shows which weeks and dates the Ericsson Cup competition runs on.";
	echo "<BR><BR>A random draw is undertaken at the start of the season where you will be drawn to play another ";
	echo "team in round 1. Whichever team scores the most points for that week will progress to the next round ";
	echo "where you will be drawn again against another team. If points are level then the home team progresses. Teams score points as normal for FA Cup and Premiership fixtures on ";
	echo "that particular week. the winner of the final receives £25.";
	echo "<BR><BR>";
	echo "<table>";
	echo "<tr>";
	echo "<th>Round</th><th>From</th><th>(Week)</th><th>To</th>";
	echo "</tr>";

	for ($round = 1; $round <= $numberRounds; $round++)
	{
		if ($month > 12)
		{
			// It's next year
			$monthName = $cDates->getMonthName($month - 12);
		}
		else
		{
			$monthName = $cDates->getMonthName($month);
		}
		
		echo "<tr>";
		if ($round == CalculateNumberRounds($numMembers))
		{
			echo TD_NAME."Final</td>";
		}
		else if ($round == (CalculateNumberRounds($numMembers)-1))
		{
			echo TD_NAME."Semi Finals</td>";
		}
		else if ($round == (CalculateNumberRounds($numMembers)-2))
		{
			echo TD_NAME."Quarter Finals</td>";
		}
		else
		{
			echo TD_NAME.$round."</td>";
		}
		
		
		echo TD_NAME.date("d-m-Y",$cDates->getStartDateFromWeek($weekArray[$round]))."</td>";
		echo TD_NAME.$weekArray[$round]."</td>";
		echo TD_NAME.date("d-m-Y",$cDates->getEndDateFromWeek($weekArray[$round]))."</td>";
		
		
		echo "</tr>";
	}

	echo "</table>";
}

function CalculateNumberRounds($numMembers)
{
	if ($numMembers > 64)
		return 7;
	else if ($numMembers > 32)
		return 6;
	else if ($numMembers > 16)
		return 5;
	else if ($numMembers > 8)
		return 4;
	else if ($numMembers > 4)
		return 3;
	else if ($numMembers > 2)
		return 2;
	else
		return 1;
}

echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?> 
